package com.ruoyi.project.module.task;

import com.alibaba.druid.wall.violation.ErrorCode;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.project.module.connector.service.IConnectorService;
import com.ruoyi.project.module.errorcode.domain.Errorcode;
import com.ruoyi.project.module.errorcode.service.IErrorcodeService;
import com.ruoyi.project.module.errorcodeCache.domain.ErrorcodeCache;
import com.ruoyi.project.module.errorcodeCache.service.IErrorcodeCacheService;
import com.ruoyi.project.module.project.domain.Project;
import com.ruoyi.project.module.project.service.IProjectService;
import com.ruoyi.project.module.task.mapper.TaskUtilsMapper;
import com.ruoyi.project.module.utils.IpUtils;
import com.ruoyi.project.module.utils.ProjectUtils;
import org.shoukaiseki.apiview.common.util.Base62Decimal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sound.midi.Soundbank;
import java.util.Date;
import java.util.List;

/**
 **/
@Component("errorCodeCacheTask")
public class ErrorCodeCacheTask {



    @Resource
    TaskUtilsMapper taskUtilsMapper;

    @Autowired
    private IConnectorService connectorService;

    @Autowired
    private IErrorcodeService errorcodeService;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private IErrorcodeCacheService errorcodeCacheService;

    public static final Logger logger= LoggerFactory.getLogger("org.shoukaiseki.task.errorcode.cache");

    public void updateErrorCodeCacheNoParams()  {
        logger.info("-------------------updateErrorCodeCacheNoParams");
        Date nowTime=taskUtilsMapper.getCurrentDate();
        while (true){
            //每次查询50条进行处理
            PageHelper.startPage(1, 50,"api_errorcode_id" );
            List<Errorcode> list = errorcodeService.selectErrorcodeUserInTask(null);
            long total = ((Page) list).getTotal();
            if(list.isEmpty()){
                break;
            }
            int i=0;
            for (Errorcode apiErrorcode:list ) {
                logger.debug("{}[{}]------{},{}",total,(total-i),apiErrorcode.getApiErrorcodeId(),apiErrorcode.getDescription());
                ++i;
                int projectId = apiErrorcode.getApiProjectnameId();
                Integer errorGroup=null;
                Project project = projectService.selectProjectById(projectId);
                ErrorcodeCache errorcodeCache=new ErrorcodeCache();
                BeanUtils.copyProperties(apiErrorcode,errorcodeCache);
                errorcodeCache.setErrorcode(apiErrorcode.getErrorcode());
                errorcodeCache.setCreateBy("admintask");
                //如果获取不到 errorGroup 则不更新相关字段
                if(project!=null){
                    errorGroup = ProjectUtils.getErrorGroupByProject(projectService, project);
                    if(errorGroup==null){
                        errorGroup=0;
                    }
                    Integer errorcode = apiErrorcode.getErrorcode();
                    int codeBase10 = (errorGroup << 12) + errorcode;
                    errorcodeCache.setErrorgroup(errorGroup);
                    errorcodeCache.setCodeBase10(codeBase10);
                    errorcodeCache.setCodeBase62(Base62Decimal.convertDecimalToBase62ToString(codeBase10));
                }
                String ipAddress = IpUtils.getIpAddress();
                //更新用户使用ip地址,因为该表只有定时任务才会修改
                errorcodeCache.setUpdateBy(ipAddress);
                errorcodeCache.setSourceUpdateTime(apiErrorcode.getUpdateTime());
                System.out.println("errorcodeCache.getUpdateTime="+errorcodeCache.getUpdateTime());

                if(errorcodeCacheService.selectErrorcodeCacheById(errorcodeCache)==null){
                    errorcodeCacheService.insertErrorcodeCache(errorcodeCache);
                }else{
                    errorcodeCacheService.updateErrorcodeCache(errorcodeCache);
                }
            }
        }
    }

}
